package com.jz.community.modulemine.packet.ui.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.packet.bean.RedPacketBean;
import com.ms.square.android.expandabletextview.ExpandableTextView;

import java.util.List;

public class RedPacketAdapter extends BaseQuickAdapter<RedPacketBean.EmbeddedBean.RedPacketLogsBean, BaseViewHolder> {
    private int status;

    public RedPacketAdapter(int layoutResId, @Nullable List<RedPacketBean.EmbeddedBean.RedPacketLogsBean> data) {
        super(layoutResId, data);
    }

    public RedPacketAdapter(int layoutResId, @Nullable List<RedPacketBean.EmbeddedBean.RedPacketLogsBean> data, int status) {
        super(layoutResId, data);
        this.status = status;
    }

    @Override
    protected void convert(BaseViewHolder helper, RedPacketBean.EmbeddedBean.RedPacketLogsBean entity) {
        LinearLayout top_ll = helper.getView(R.id.item_packet_top_ll);
        LinearLayout bot_ll = helper.getView(R.id.item_packet_bot_ll);
        ImageView img = helper.getView(R.id.item_packet_img);
        TextView price = helper.getView(R.id.item_packet_price);

        TextView rule = helper.getView(R.id.item_packet_rule);
        ExpandableTextView ex_tv = helper.getView(R.id.item_packet_expand);

        int hasLimit = 1;

        StringBuilder sb = new StringBuilder();

        if (!RxDataTool.isNullString(entity.getLimitshopname())) {
            //限制店铺
            hasLimit = 0;
            sb.append("指定店铺适用: ").append(entity.getLimitshopname());
            sb.append("\n");
        }

        if (!RxDataTool.isNullString(entity.getLimitgoodsnames())) {
            //限制商品
            hasLimit = 0;
            sb.append("指定商品适用: ").append(entity.getLimitgoodsnames());
            sb.append("\n");
        }


        if (RxDataTool.isNullString(entity.getLimitshopname()) && RxDataTool.isNullString(entity.getLimitgoodsnames())) {
            //无限制
            hasLimit = 1;
        }

        if (hasLimit == 0) {
            //有限制
            ex_tv.setText(sb.substring(0, sb.length() - 1));
        } else {
            //无限制
            ex_tv.setText("全场适用");
        }

        if (status == 0) {
            //可使用
            top_ll.setBackgroundResource(R.drawable.border_packet_top_red_line);
            bot_ll.setBackgroundResource(R.drawable.border_packet_bot_red_line);
            img.setImageResource(R.mipmap.packet_red);
            price.setTextColor(mContext.getResources().getColor(R.color.font_black));
        } else if (status == 1) {
            //已过期
            top_ll.setBackgroundResource(R.drawable.border_packet_top_gray_line);
            bot_ll.setBackgroundResource(R.drawable.border_packet_bot_gray_line);
            img.setImageResource(R.mipmap.packet_gray);
            price.setTextColor(mContext.getResources().getColor(R.color.font_gary));
        }

        helper.setText(R.id.item_packet_name, entity.getName());
        String startTime = entity.getStartdate();
        String endTime = entity.getEnddate();
        helper.setText(R.id.item_packet_time, startTime + "至" + endTime);
        price.setText("¥" + entity.getMoney());

        if (entity.getMinlimitmoney() != 0) {
            //是否满足满多少可用的限制
            rule.setText("满¥" + entity.getMinlimitmoney() + "可用");
        } else {
            rule.setText(" ");
        }


    }
}
